﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jungle.Rent;
using Jungle.Code.Core;
using Jungle.Rent;
using Jungle.Users;
using Jungle.Code.Utils;



public partial class SearchResult : System.Web.UI.UserControl
{
    private int OnPage = 10;
    private int CurrPage = 1;

    private int OfferId = 0;
    private int UnderType = 0;
    private int RegionId;
    private string City;
    private int OfferType;
    private double ObjectSquare1;
    private double ObjectSquare2;
    private double ObjectSquare3;
    private double ObjectSquare4;
    private double Price1;
    private double Price2;
    private int MoneyType = 0;
    private bool Photo;
    private bool Komiss;
    private bool NewBuild;
    private string ComUnder2 = "";

    //------------------------------------------------------------------------------------------------------
    protected void LoadRequest()
    {
        HttpCookie cookie = Request.Cookies["SearchOptionsAlexConsult6"];
        if (cookie != null)
        {
            try { OfferId = Convert.ToInt32(cookie["op00"]); }
            catch { OfferId = 0; }

            if (cookie["op01"].Length > 3) ComUnder2 = cookie["op01"].ToString().Trim();
            else
                UnderType = Convert.ToInt32(cookie["op01"]);
            RegionId = Convert.ToInt32(cookie["op02"]);
            City = cookie["op022"];
            OfferType = Convert.ToInt32(cookie["op03"]);
            ObjectSquare1 = Convert.ToDouble(cookie["op04"]);
            ObjectSquare2 = Convert.ToDouble(cookie["op05"]);
            ObjectSquare3 = Convert.ToDouble(cookie["op06"]);
            ObjectSquare4 = Convert.ToDouble(cookie["op07"]);
            Price1 = Convert.ToDouble(cookie["op08"]);
            Price2 = Convert.ToDouble(cookie["op09"]);
            //MoneyType = Convert.ToInt32(cookie["op10"]);

            Photo = Convert.ToBoolean(cookie["op11"]);
            Komiss = Convert.ToBoolean(cookie["op12"]);
            NewBuild = Convert.ToBoolean(cookie["op13"]);
        }        
    }
    //-----------------------------------------------------------------------------------------------
    public void RaisePostBackEvent(string eventArgument)
    {
        int pos = eventArgument.IndexOf(',');
        int l = eventArgument.Length;
        string s1 = eventArgument.Substring(0, pos);
        string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

        if (s1 == "chP") // перелистывание страницы
        {
            CurrPage = Convert.ToInt32(s2);
        }
    }
    //-----------------------------------------------------------------------------------------------
    protected override void Render(HtmlTextWriter writer)
    {
        try
        {
            LoadRequest();
            if (OfferId > 0)
            {
                Offer of = new Offer();
                of.OfferId = OfferId; of.GetById();
                GetOfferHTML(writer, of);
                Response.Write("asd");
            }
            else
            {
                //Response.Write(UnderType + "<br/> " + RegionId + "<br/> " + City + "<br/> " + OfferType + "<br/> " + ObjectSquare1 + "<br/> " + ObjectSquare2 + "<br/> " + ObjectSquare3 + "<br/> " + ObjectSquare4 + "<br/> " + Price1 + "<br/> " + Price2 + " <br/>" + MoneyType + "<br/> " + Photo + " <br/>" + NewBuild + " <br/>" + Komiss + "<br/> " + ComUnder2);
                List<Offer> SearchResult = Offer.Search(UnderType, RegionId, City, OfferType, ObjectSquare1, ObjectSquare2, ObjectSquare3, ObjectSquare4, Price1, Price2, MoneyType, Photo, NewBuild, Komiss, ComUnder2);

                int count = 0;
                if (SearchResult.Count == 0)
                {
                    writer.Write("По вашему запросу ничего не найдено.");
                }
                else
                {
                    //Response.Write(SearchResult.Count);
                    writer.Write("<div style=\"font-size: 180%; padding-left: 20px;\">Результаты поиска</div>");
                    for (int i = (OnPage * CurrPage) - OnPage; i <= (OnPage * CurrPage) - 1; i++)
                    {
                        if (i >= SearchResult.Count) break;
                        GetOfferHTML(writer, SearchResult[i]);
                    }

                    if (SearchResult.Count % OnPage == 0)
                        RenderPageLister(writer, SearchResult.Count / OnPage);
                    else
                        RenderPageLister(writer, (SearchResult.Count / OnPage) + 1);
                }
            }
            
            
        }
        catch (Exception es) { Response.Write(es.Message); }
        base.Render(writer);
    }
    //--------------------------------------------------------------------
    public HtmlTextWriter GetOfferHTML(HtmlTextWriter writer, Offer o)
    {
        string imgUrl = UrlHelper.GetSiteUrl();
        int topMove = 0;
        if (o.Photo.Length > 10)
        {
            string ff = "";
            if (o.Photo.IndexOf(",") != -1)
            {
                ff = o.Photo.Substring(0, o.Photo.IndexOf(","));
                imgUrl += ff;
            }
            else
            {
                ff = o.Photo;
                imgUrl += ff;
            }
            System.Drawing.Image img = System.Drawing.Image.FromFile(Context.Server.MapPath(ff));
            if (img.Height > 69) topMove = (-1) * (img.Height - 69) / 2;
        }
        else
            imgUrl += "/App_Themes/alex/img/el/nophoto_mini.jpg";
        writer.AddAttribute("class", "preItem");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        writer.AddAttribute("class", "bg_preImg");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.AddAttribute("class", "preImg");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundImage, imgUrl);
        writer.AddStyleAttribute("background-position", "0px " + topMove.ToString() + "px");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.RenderEndTag();
        writer.RenderEndTag();

        writer.AddAttribute("class", "pre_descr");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        writer.Write(GetShortDescription(o));
        writer.RenderEndTag();

        writer.RenderEndTag();

        return writer;
    }
    //-----------------------------------------------------------------------------------------------
    public string GetShortDescription(Offer o)
    {
        string result = "";
        string a0 = "";

        if (o.OfferType) a0 = "<b>Продажа "; else a0 = "<b>Аренда ";
        if (o.ObjectType < 4)
            result = a0 += Offer.GetObjectTypeTitle(o.UnderTypeId) + "</b>";
        if (o.ObjectType == 4)
            result = a0 + " гаража</b>";
        if (o.ObjectType == 5)
            result = a0 + " участка земли</b>";

        if (o.Komiss) result += "&nbsp;&nbsp;<span style=\"color: Green; font-weight: bold;\">Без комиссии!</span>";

        result += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"" + UrlHelper.GetSiteUrl() + "/ru/offers.aspx?cmd=show&id=" + o.OfferId.ToString() + "\" style=\"color: #666; font-size: 80%; font-weight: bold;\">[Подробнее]</a>";

        result += "<h1>";
        if (o.City.Length > 0)
        {
            result += "<br/>" + Offer.ddl_Region().Items.FindByValue(o.RegionId.ToString()).Text + " г." + o.City + " ул." + o.Street + " " + o.HomeNumber;
        }
        result += "</h1>";

        int lenght = 160;
        if (o.Description.Length < lenght) lenght = o.Description.Length;
        result += o.Description.Substring(0, lenght);
        return result;
    }
    //-----------------------------------------------------------------------------------------------
    public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
        writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
        writer.RenderBeginTag(HtmlTextWriterTag.Img);
        writer.RenderEndTag();
        return writer;
    }
    //-----------------------------------------------------------------------------------------------
    private HtmlTextWriter RenderPageLister(HtmlTextWriter writer, int count)
    {
        ClientScriptManager cs = Page.ClientScript;
        writer.Write("<b>Страница: </b>");
        for (int i = 1; i <= count; i++)
        {
            if (i == CurrPage)
                writer.Write("<b>" + i.ToString() + "</b>");
            else
            {
                PostBackOptions opt = new PostBackOptions(this);
                opt.ActionUrl = Context.Request.RawUrl;
                opt.Argument = "chP," + i.ToString();

                writer.AddAttribute("onclick", cs.GetPostBackEventReference(opt));
                writer.AddAttribute("style", "cursor: pointer;");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(i.ToString());
                writer.RenderEndTag();
            }
            if (i < count) writer.Write(", ");
        }
        return writer;
    }
    //------------------------------------------------------------------------------------------------------
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }
    // ---------------------------------------------------------------------

}
